<?php
include_once (SYSTEM.'/Controller.php');
include_once (MODEL . '/ExchangeRepository.php');

class ExchangeController extends Controller
{
	public function Index()
	{
		$this->ViewData['Title'] = 'exchange';
		$UserAccountID = "4fdee32a6803fa610b000000";
		
		$exchange = new ExchangeRepository();
		$params =$this->GetParams();
		$this->ViewData['Avatar'] = $UserAccountID.".jpg";
		$isNewUser = true;
		$productID = $params['offerID'];
		if(isset($params['sbm_comment']))
		{
			$conversaID = $params['converID'];
			$content = $params['comment'];
			$exchange->PutComment($content, $conversaID, $UserAccountID,$productID);
		}
		
		if (isset($params['sbm_Conver'])) {
			$firstContent = $params['firstContent'];
			$exchange->PutConver($firstContent, $UserAccountID, $productID);
		}
		$converInform = $exchange->ProductInforms($productID);
		$model['ProductInform'] =$converInform;
		if (isset($params['sbm_Conversa'])) {
			$convrsaID = $params['conversaID'];
			
			if ($converInform->NumberOfPart >$converInform->hasPart) {
				
				if(($converInform->NumberOfPart)-($converInform->hasPart)==1)
				{
					$exchange->MakeMatch($productID);
				}
				$numPart = $converInform->hasPart +1;
			}
			else {
				$numPart = $converInform->NumberOfPart;
			}
			
			$exchange->PutCheck($convrsaID, $numPart, $productID);
		}
		
		$this->ViewData['userType'] =0;		
		$OwnerID = $exchange->GetOwnerByOfferID($productID);
		
		$this->ViewData['offerId'] = $productID;
		if($UserAccountID==$OwnerID)
		{
			$this->ViewData['userType'] =1;
		}
		
		$listConversation = $exchange->GetConversation($productID,$UserAccountID);
		foreach ($listConversation as $conversation)
		{
			if($conversation->IsCommenter)
			{
				$isNewUser = false;
			}
		}
		$this->ViewData['newUser'] = $isNewUser;
		$model['ListConversation'] = $listConversation;
		
		$this->ViewData['numCmt'] = $exchange->GetNumComment($productID);
	    $this->Render('index','_Master',$model);
	}
	
	public function ListExchange()
	{
		$exchange  = new ExchangeRepository();
		$model['listExchange'] = $exchange->GetListExchange(8);
		$this->Render('ListExchange', '_Master',$model);
	}
	
}